package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.pojo.Res;
import com.itheima.health.service.ResCheckoutService;
import com.tencentcloudapi.captcha.v20190722.CaptchaClient;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultRequest;
import com.tencentcloudapi.captcha.v20190722.models.DescribeCaptchaResultResponse;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;

import java.util.Objects;

@Service
public class ResCheckoutServiceImpl implements ResCheckoutService {

    @Override
    public boolean resCheckout(Res res,String userIp){
        try{
            Credential cred = new Credential("AKIDnT2K1QBYdQxQSCqKnyoMYR8OMBn23KBe", "JMDgsoDUS3LWSgilaLp3RWdP85kVyy98");

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("captcha.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            CaptchaClient client = new CaptchaClient(cred, "", clientProfile);

            String params = "{\"CaptchaType\":9,\"Ticket\":\""+ res.getTicket() +"\",\"UserIp\":\""+userIp+"\",\"Randstr\":\""+ res.getRandstr() +"\",\"CaptchaAppId\":2007470352,\"AppSecretKey\":\"0gRA1NKcGvxCQWj1mDZ5_Jg**\"}";
            DescribeCaptchaResultRequest req = DescribeCaptchaResultRequest.fromJsonString(params, DescribeCaptchaResultRequest.class);

            DescribeCaptchaResultResponse resp = client.DescribeCaptchaResult(req);

            System.out.println(DescribeCaptchaResultResponse.toJsonString(resp));
            String captchaMsg = resp.getCaptchaMsg();

            if (Objects.equals(captchaMsg, "OK")){
                return true;
            }else {
                return false;
            }
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }

        return false;

    }


}
